// =======================================================================
// 
//       Filename:  parser.cpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  10.05.2010 16:49:12
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
//        Company:  -
// 
// =======================================================================

#include "parser.hpp"
#include "page.hpp"
#include "../exceptions/fileerror.hpp"
#include <fstream>
#include <yaml.h>
#include <iostream>

using namespace std;
using webplodder::exceptions::FileError;

namespace webplodder {
    namespace page {
        Parser::Parser() {

        }

        void Parser::parse(Page& page) {
            ifstream in(page.sourcePath().string().c_str());

            if(!in.good())
                throw FileError("failed to open",
                                page.sourcePath());

            YAML::Parser parser(in);

            YAML::Node doc;

            parser.GetNextDocument(doc);
            YAML::Iterator it;

            string key;

            for(it = doc.begin(); it != doc.end(); ++it) {
                it.first() >> key;

                if(key == "pos")
                    it.second() >> page.m_pos;
                else if(key == "in_menu")
                    it.second() >> page.m_inMenu;

                it.second() >> page[key];
            }
        }
    }
}
